package org.yty.travelagencyredis.Dao.impl;

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.yty.travelagencyredis.Dao.RouteDao;
import org.yty.travelagencyredis.model.Entity.Route;

@Repository
@RequiredArgsConstructor
public class RouteDaoImpl implements RouteDao {
    private final StringRedisTemplate redisTemplate;

    @Override
    public Route selectById(Long id) {
        String routeJson = (String) this.redisTemplate.opsForHash().get(Route.key, id);
        return JSON.parseObject(routeJson,Route.class);
    }

    @Override
    public boolean insert(Route route) {
        this.redisTemplate.opsForHash().put(Route.key, route.getId(), JSON.toJSONString(route));
        return true;
    }

    @Override
    public boolean updateById(Route route) {
        this.redisTemplate.opsForHash().put(Route.key, route.getId(), JSON.toJSONString(route));
        return true;
    }

    @Override
    public void deleteById(Long id) {
        this.redisTemplate.opsForHash().delete(Route.key, id);
    }
}
